package com.example.mymusic;

import android.content.Context;
import android.database.Cursor;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.provider.MediaStore;

import java.util.ArrayList;

import androidx.annotation.RequiresApi;

/**
 * @author xky
 */
public class Utils {
    /**
     * 获取音乐，放到数组列表中
     */
    @RequiresApi(api = Build.VERSION_CODES.Q)
    public static ArrayList<Music> getMusic(Context context) {
        ArrayList<Music> listMusic = new ArrayList<>();

        //扫描遍历外部存储中的音乐
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Music music = new Music();
                long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                String singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));

                if ("<unknown>".equals(singer) || singer == null) {
                    singer = context.getResources().getString(R.string.name_default);
                }
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
                int albumId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

                music.setId(id);
                music.setDuration(duration);
                music.setName(name);
                music.setPath(path);
                music.setSinger(singer);
                music.setSize(size);
                music.setAlbum(album);
                music.setAlbumId(albumId);
                //用music里的方法获取数据放到listMusic中
                listMusic.add(music);
            }
        }
        assert cursor != null;
        cursor.close();
        return listMusic;
    }

    public static byte[] getCover(String path) {
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(path);
        return mediaMetadataRetriever.getEmbeddedPicture();
    }

    public static String formatTime(int time) {

        String tt;
        int thousand = 1000;
        int minute = 60;
        int ten = 10;

        if (time / thousand % minute < ten) {
            tt = time / thousand / minute + ":0" + time / thousand % minute;
        } else {
            tt = time / thousand / minute + ":" + time / thousand % minute;
        }
        return tt;
    }


}